<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class AdsController extends Controller {

    public function ads_list() {//轮播图列表页面的显示
        $data = \DB::table("ads")->paginate(5);
        return view('ads.ads_list')->with("data", $data);
    }

    public function ads_add() { //轮播图添加页面的显示
        return view('ads.ads_add');
    }

    public function edit($id) { //轮播图修改页面的显示
        $data = \DB::table("ads")->where("id", $id)->first();
        return view('ads.ads_edit')->with("data", $data);
    }

    public function ads_edit(Request $request) { //轮播图处理修改
        $str = $_POST; //获取表单提交的所有数据  
        unset($str['_token']); //去除token 
        //处理上传的图片
        $file = $request->file('photo'); //获取主图片 
        if (empty($file)) {
            if (\DB::table("ads")->where("id", $str['id'])->update($str)) {
                return redirect('/admin/ads/ads_list'); //修改成功跳转到商品列表 
            } else {
                echo "修改失败";
            }
        } else {
            $m_size = 1048576; //限制上传图片的大小单位字节，现在大小是不得超过1M 
            $allowed_extensions = ["png", "jpg", "JPG", "gif"];
            $filedir = "uploads/lun/" . date('Y-m-d', time()); //2、定义图片上传路径  
            $path = $this->upload($file, $m_size, $allowed_extensions, $filedir);
            $str['img'] = $path;
            if (\DB::table("ads")->where("id", $str['id'])->update($str)) {
                return redirect('/admin/ads/ads_list'); //修改成功跳转到商品列表 
            } else {
                echo "修改失败";
            }
        }
    }

    public function del($id) { //删除
        if (\DB::table("ads")->where("id", $id)->delete()) {
            return 1;
        } else {
            return 0;
        }
    }

    public function upload($file, $m_size, $allowed_extensions, $filedir) {

        if ($file->getClientOriginalExtension() && !in_array($file->getClientOriginalExtension(), $allowed_extensions)) {
            return "<script>alert('只能上传png，jpg，gif格式');history.go(-1);location.reload(); </script>";
        }
        if ($file->getClientSize() > $m_size) {
            return "<script>alert('主图图片大小不能超过1M');history.go(-1);location.reload(); </script>";
        }
        $fileName = $file->getClientOriginalName(); //3、获取上传图片的文件名  
        $extension = $file->getClientOriginalExtension(); //4、获取上传图片的后缀名  
        $newImagesName = md5(time()) . "." . $extension; //5、重新命名上传文件名字 
        $file->move($filedir, $newImagesName); //6、使用move方法移动文件 
        $path = "/" . $filedir . "/" . $newImagesName; //定义数据库储存的路径
        return $path; //将图片路径插入到表单提交的数组当中
    }

    public function store(Request $request) {//轮播图添加的处理
        $str = $_POST; //获取表单提交的所有数据  
        unset($str['_token']); //去除token 
        //处理上传的图片
        $file = $request->file('photo'); //获取主图片 
        $m_size = 1048576; //限制上传图片的大小单位字节，现在大小是不得超过1M 
        $allowed_extensions = ["png", "jpg", "JPG", "gif"];
        $filedir = "uploads/lun/" . date('Y-m-d', time()); //2、定义图片上传路径  
        $path = $this->upload($file, $m_size, $allowed_extensions, $filedir);
        $str['img'] = $path;
        $data = \DB::table("ads")->insert($str);
        $id = \DB::getPdo()->lastInsertId(); //获取刚刚插入产品的id 
        if ($id) {
            return redirect('/admin/ads/ads_list'); //添加成功跳转到商品列表 
        }
    }

}
